package com.dhms.vostok1.adapter

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import coil.load
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dhms.mine.R
import com.dhms.vostok1.data.select_mbr.*

class SelectMemberAdapter : BaseMultiItemQuickAdapter<MultiItemInfo, BaseViewHolder>() {

    private var gListener: ((CompanyGroupBean) -> Unit)? = null
    private var mListener: ((MultiItemInfo) -> Unit)? = null
    var iSearch = false
    var searchKey = ""

    init {
        addItemType(TYPE_MEMBER, R.layout.member_item)
        addItemType(TYPE_GROUP, R.layout.group_item)
    }


    override fun convert(holder: BaseViewHolder, item: MultiItemInfo) {
        when (item.itemType) {
            0 -> {
                holder.getView<ImageView>(R.id.img_avatar).load("") {
                    placeholder(R.drawable.ic_avatar_placeholder)
                    error(R.drawable.ic_avatar_placeholder)
                }
                val checkBox = holder.getView<CheckBox>(R.id.check_box)
                holder.getView<View>(R.id.root_view).setOnClickListener {
                    checkBox.isChecked = !checkBox.isChecked
                }
                checkBox.setOnCheckedChangeListener { _, isChecked ->
                    item.isChecked = isChecked
                    mListener?.let {
                        it(item)
                    }
                }
                checkBox.isChecked = item.isChecked
                if (iSearch) {
                    item.mbr?.let {
                        val span = SpannableString(it.us_name)
                        val startIndex = it.us_name.indexOf(searchKey)
                        val endIndex = it.us_name.indexOf(searchKey) + searchKey.length
                        if (startIndex >= 0 && endIndex >= 0) {
                            span.setSpan(
                                ForegroundColorSpan(Color.parseColor("#006CB8")),
                                startIndex,
                                endIndex,
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                            )
                        }
                        holder.setText(R.id.tv_name, span)
                    }
                    checkBox.isEnabled = !item.isChecked
                } else {
                    holder.setText(R.id.tv_name, item.mbr?.us_name)
                }
            }
            1 -> {
                holder.setText(R.id.tv_group_name, item.groupBean?.name)
                holder.getView<View>(R.id.item_view).setOnClickListener {
                    gListener?.let {
                        item.groupBean?.let { it1 -> it(it1) }
                    }
                }
                val position = getItemPosition(item)
                if (position + 1 < itemCount && getItem(position + 1).groupBean == null) {
                    holder.setVisible(R.id.deliver1,false)
                    holder.setVisible(R.id.deliver2,true)
                } else {
                    holder.setVisible(R.id.deliver1,true)
                    holder.setVisible(R.id.deliver2,false)
                }
            }
        }
    }

    fun setGroupItemClickListener(listener: (CompanyGroupBean) -> Unit) {
        gListener = listener
    }

    fun setMemberSelectListener(listener: (MultiItemInfo) -> Unit) {
        mListener = listener
    }


}